package com.wanyang.aliyun.service;

import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.wanyang.aliyun.configure.AccessProperties;
import com.wanyang.aliyun.configure.OSSProperties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.UUID;

@Service
public class OssService {
    @Autowired
    private OSSProperties ossProperties;

    @Autowired
    private AccessProperties accessProperties;

    public String upload(MultipartFile multipartFile, String onlinePath) {
        try {
            return upload(multipartFile.getInputStream(), multipartFile.getContentType(), onlinePath);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    public String upload(InputStream inputStream, String contentType, String onlinePath) {
        OSS client = new OSSClient(ossProperties.getEndpoint(), accessProperties.getAccessKeyId(), accessProperties.getAccessKeySecret());
        PutObjectResult result = null;
        String randomFileName = onlinePath + "/" + UUID
                .randomUUID()
                .toString();

        ObjectMetadata objectMetadata = null;
        if (StringUtils.isNotBlank(contentType)) {
            objectMetadata = new ObjectMetadata();
            objectMetadata.setContentType(contentType);

        }

        if (objectMetadata != null) {
            result = client.putObject(
                    ossProperties.getDefaultBucketName(),
                    randomFileName,
                    inputStream, objectMetadata
            );

        } else {
            PutObjectRequest putObjectRequest = new PutObjectRequest(ossProperties.getDefaultBucketName(), randomFileName, inputStream, null);
            putObjectRequest.setProcess("");
            result = client.putObject(putObjectRequest);
        }

        client.shutdown();

        return getOnlineBasePath() + "/" + randomFileName;
    }

    public String upload(MultipartFile multipartFile) {
        return upload(multipartFile, "default");
    }

    private String getOnlineBasePath() {
        return ossProperties.getOnlineBasePath();
    }

    public String getObjectName(String url) {
        return url.replace(ossProperties.getOnlineBasePath() + "/", "");
    }

    public String processVideo(String objectName) {
        // 创建OSSClient实例。
        OSS ossClient = new OSSClient(ossProperties.getEndpoint(), accessProperties.getAccessKeyId(), accessProperties.getAccessKeySecret());
        // 设置视频截帧操作。
        String style = "video/snapshot,t_50000,f_jpg,w_500,h_800";
        // 指定过期时间为10分钟。
        Date expiration = new Date(new Date().getTime() + 1000 * 60 * 10);
        GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(ossProperties.getDefaultBucketName(), objectName, HttpMethod.GET);
        req.setExpiration(expiration);
        req.setProcess(style);
        URL signedUrl = ossClient.generatePresignedUrl(req);
        System.out.println(signedUrl);
        // 关闭OSSClient。
        ossClient.shutdown();


        try {
            return upload(signedUrl.openStream(), null, "default");
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

}
